package jjoms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jjoms.model.board.BoardService;
import jjoms.model.board.BoardVO;
import jjoms.model.board.ComentVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/board")
public class BoardController  extends MultiActionController{
@Resource(name = "boardServiceImpl")
	private BoardService boardService;
	private Log log=LogFactory.getLog(getClass());
/*@RequestMapping("/{viewId}.moja")
public String showView(@PathVariable("viewId") String viewId) {
	//System.out.println(viewId+"_view.jsp 가 메인으로 응답");		
	return viewId+".board";
}*/
	
	@RequestMapping("auth_writeview.moja")
	public ModelAndView writeview(HttpServletRequest request, HttpServletResponse response){
		System.out.println("글쓰기 실행");
		return new ModelAndView("write_view.board");
	}
@RequestMapping(value="/register.moja",method=RequestMethod.POST)	
public ModelAndView register(HttpServletRequest request, HttpServletResponse response,BoardVO bvo){
	System.out.println("register"+bvo);
	boardService.register(bvo);
	return new ModelAndView("redirect:/board/showCountNoHit.moja?no="+bvo.getBoard_no());
}
@RequestMapping("showContent.moja")
public ModelAndView showcontent(@CookieValue(value="jjoms",required=false) String cookieValue,HttpServletRequest request, HttpServletResponse response,int no){
BoardVO bvo = null;
	if(cookieValue==null){
		log.info("jjoms cookie 존재하지 않으므로 cookie 생성하고 count 증가");
		response.addCookie(new Cookie("jjoms","|"+no+"|"));
		bvo=boardService.showcontent(no);
	}else if(cookieValue.indexOf("|"+no+"|")==-1){
		log.info("jjoms cookie 존재하지만 "+no+"번글은 처음 조회하므로 count증가");
		cookieValue+="|"+no+"|";
		//글번호를 쿠키에 추가 
		response.addCookie(new Cookie("jjoms",cookieValue));
		bvo=boardService.showcontent(no);
	}else{
		log.info("jjoms cookie 존재하고 이전에 해당 게시물 읽었으므로 count 증가x");
		bvo=boardService.showCountNoHit(no);
	}		
	return new ModelAndView("show_Content.board","bvo",bvo);
}
@RequestMapping("getboardlist.moja")
public ModelAndView getboardlist(HttpServletRequest request, HttpServletResponse response,String pageNo,BoardVO bvo){
	System.out.println("getboardlist 실행" + pageNo);
	return new ModelAndView("getBoardList.board","lvo",boardService.getboardlist(pageNo));
}
@RequestMapping("auth_update.moja")
public ModelAndView update(HttpServletRequest request, HttpServletResponse response, int no){
	System.out.println("update 실행전 :"+ no);
	
	BoardVO vo = (BoardVO)boardService.showCountNoHit(no);
	return new ModelAndView("update.board","bvo",vo);
	
}
@RequestMapping("auth_updateview.moja")
public ModelAndView updateview(HttpServletRequest request, HttpServletResponse response,BoardVO vo){
System.out.println("updateview no : "+vo);
boardService.update(vo);
	BoardVO bvo = (BoardVO)boardService.showCountNoHit(vo.getBoard_no());
System.out.println( "update 전 : " +vo);	
	return new ModelAndView("show_Content.board","bvo",bvo);
}
@RequestMapping("auth_delete.moja")
public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, int no){
	boardService.delete(no);
	System.out.println("삭제 :" + no);
	return new ModelAndView("redirect:getboardlist.moja");
}
@RequestMapping(value="auth_coment.moja",method=RequestMethod.POST)
@ResponseBody
public List<ComentVO> coment(HttpServletRequest request, HttpServletResponse response, ComentVO cvo){
	System.out.println("코멘트 정보넘기기"+cvo);
		int comentCount = boardService.getComentCount(cvo.getBoard_no());
		System.out.println("commentCount : " + comentCount);
		if(comentCount > 0){
			System.out.println("if start");
			boardService.coment(cvo);
			System.out.println("if success");
		}else{
			System.out.println("else start");
			boardService.noCountComent(cvo);
			System.out.println("else success");
		}
		System.out.println("코멘정보넘겻다!!"+cvo);
		List<ComentVO> ccvo = boardService.comentview(cvo.getBoard_no());
		System.out.println("코멘트정보가져와라"+ccvo);
		return ccvo;
	}
@RequestMapping("comentview.moja")
@ResponseBody
public List<ComentVO> comentview(HttpServletRequest request, HttpServletResponse response,int no){
System.out.println("comentview no : "+no);
List<ComentVO> ccvo = boardService.comentview(no);
System.out.println(ccvo);	
	return ccvo;
}
@RequestMapping("showCountNoHit")
public ModelAndView showCountNoHit(HttpServletRequest request, HttpServletResponse response, int no){
	return new ModelAndView("show_Content.board","bvo",boardService.showCountNoHit(no));
}
}


